/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: UserServiceImpl.java
 * Author: ckj
 * Date: 2024/7/27 下午5:20
 * Description:
 * 用户service实现
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.quickdemo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.quickdemo.constant.GlobalConstant;
import com.quickdemo.mappers.UserMapper;
import com.quickdemo.pojo.dto.IdVersion;
import com.quickdemo.pojo.dto.UserDTO;
import com.quickdemo.pojo.dto.UserPageQueryDTO;
import com.quickdemo.pojo.entity.User;
import com.quickdemo.result.PageResult;
import com.quickdemo.service.UserService;
import com.quickdemo.utils.IdGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: UserServiceImpl
 * @description: 解决什么问题：
 * 演示线程池的正确使用
 * 主要方法和数据成员：
 * methodxx 使用 ThreadPoolExecutor创建线程进行任务操作
 * 注意事项：
 * 例如：该类需要从容器获取不可以new
 * @author: ckj
 * @date: 2024/7/27下午5:20
 * @since 1.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final IdGenerator idGenerator;

    @Override
    public void add(UserDTO userDTO) {
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);

        user.setId(idGenerator.snowflakeId());

        user.setVersion(idGenerator.snowflakeId());
        user.setStatus(GlobalConstant.ROW_DATA_STATUS_ACTIVE);
        log.info("test dev-tools");
        log.info(user.toString());
        userMapper.insert(user);
    }

    @Override
    public PageResult pageQuery(UserPageQueryDTO userPageQueryDTO) {

        PageHelper.startPage(userPageQueryDTO.getPageIndex(),userPageQueryDTO.getPageSize());

        Page<User> page = userMapper.pageQuery(userPageQueryDTO);
        Long total = page.getTotal();
        List<User> list = page.getResult();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();

        Integer pages = page.getPages();
        log.info(page.toString());
        return new PageResult(total,pageNum,pageSize,pages,list);
    }

    @Override
    public UserDTO findById(Long id) {
        User user = userMapper.findById(id);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);
        return userDTO;
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(IdVersion[] idVersions) {
        List<Long> ids = Arrays.stream(idVersions)
                .map(IdVersion::getId)
                .collect(Collectors.toList());

        log.info("delete ids:{}",ids);
        userMapper.deleteByIds(ids);

    }

    @Override
    public void update(UserDTO userDTO) {
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);
        userMapper.update(user);
    }
}
